import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart';
import 'package:demo/views/chat/chat_message_card.dart';
import 'package:demo/views/chat/chat_message_file.dart';
import 'package:demo/views/chat/chat_message_image.dart';
import 'package:demo/views/chat/chat_message_other.dart';
import 'package:demo/views/chat/chat_message_text.dart';
import 'package:demo/views/chat/chat_message_tips.dart';
import 'package:demo/views/chat/chat_message_video.dart';
import 'package:demo/views/chat/chat_message_voice.dart';
import 'package:demo/tools/tools_enum.dart';
import 'package:demo/tools/tools_sqlite.dart';

// 聊天=消息
class ChatMessage extends StatefulWidget {
  final ChatHis chatHis;
  final AudioPlayer audioPlayer;

  const ChatMessage(
    this.chatHis,
    this.audioPlayer, {
    super.key,
  });

  @override
  createState() => _ChatMessageState();
}

class _ChatMessageState extends State<ChatMessage> {
  @override
  Widget build(BuildContext context) {
    // 历史消息
    ChatHis chatHis = widget.chatHis;
    // 消息类型
    MsgType msgType = chatHis.msgType;
    // 消息内容
    Map<String, dynamic> content = chatHis.content;
    // 结果
    Widget result;
    switch (msgType) {
      // 提示消息
      case MsgType.tips:
        result = ChatMessageTips(content);
        break;
      // 文本消息
      case MsgType.text:
        result = ChatMessageText(content);
        break;
      // 图片消息
      case MsgType.image:
        result = ChatMessageImage(
          chatHis.content,
        );
        break;
      // 视频消息
      case MsgType.video:
        result = ChatMessageVideo(
          content,
        );
        break;
      // 文件消息
      case MsgType.file:
        result = ChatMessageFile(content);
        break;
      // 声音消息
      case MsgType.voice:
        result = ChatMessageVoice(chatHis, widget.audioPlayer);
        break;
      // 名片消息
      case MsgType.card:
        result = ChatMessageCard(content);
        break;
      // 其他消息
      default:
        result = const ChatMessageOther();
        break;
    }
    return result;
  }
}
